﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace SF.Base.Core {
	public class PostRules {
		/// <summary>
		/// 查询Post
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public static List<PostEntity> GetPostsByUserId(string userId){
			List<PostEntity> Posts = new List<PostEntity>();
			try {
				using (
					IDataReader dr = Sh.ExecuteReader(
						BaseSettings.Instance.ConnSF,
						CommandType.Text,
						@"
						select 
							pid,
							post_id,
							content,
							object_id,
							object_type,
							post_date,
							CONVERT(varchar(100), post_date, 23) as post_date_day
						from dt_posts 
						where user_id = @user_id
						order by post_date desc
					",
						Sh.GetParameter("@user_id", DbType.String, userId)
					)
				) {
					while (dr.Read()) {
						PostEntity Post = new PostEntity();

						Post.Pid = dr.GetLong("pid");
						Post.PostId = dr.GetStr("post_id");
						Post.Content = dr.GetStr("content");
						Post.ObjectId = dr.GetStr("object_id");
						Post.ObjectType = dr.GetStr("object_type");
						Post.PostDate = dr.GetDate("post_date");
						Post.PostDateDay = dr.GetStr("post_date_day");
						
						Posts.Add(Post);
					}

					return Posts;
				}
			}
			catch (System.Exception e) {
				throw new Exception("查询Post失败", e);
			}
		}
	}
}
